﻿using System.Text;

namespace Sharp.Portal.AdminLTE.FormField
{
    /// <summary>
    /// html编辑字段
    /// </summary>
    public class HtmlEditorField : ElementField
    {
        /// <summary>
        /// html编辑字段
        /// </summary>
        /// <param name="fieldName"></param>
        public HtmlEditorField(string fieldName)
        {
            this.FieldName = fieldName;
        }

        /// <summary>
        /// 生成
        /// </summary>
        /// <param name="nonFirst"></param>
        /// <returns></returns>
        public override string Build(bool nonFirst = false)
        {
            if (this.IsBrowsing() && !this.KeepingXType)
            {
                return $"@Html.Bootstrap().LableFor(m => m.{this.FieldName})" + base.Build(nonFirst);
            }

            var sb = new StringBuilder();
            sb.Append("<textarea id=\"" + this.FieldName + "_editor\" type=\"text\" class=\"form-control\" cols=\"4\">@Model." + this.FieldName +
                      "</textarea>");
            sb.AppendFormat("<input id=\"{0}\" name=\"{0}\" value=\"\" type=\"hidden\">", FieldName);
            //sb.AppendLine("@Html.HiddenFor(m => m." + this.FieldName + ")");
            //sb.AppendFormat("@Html.ValidationMessageFor(m => m.{0})", FieldName);
            sb.AppendLine("<script type=\"text/javascript\">");
            sb.AppendLine("$(\"#" + this.FieldName + "_editor\").summernote({");
            sb.AppendLine("            height: 250,");
            sb.AppendLine("            lang: 'zh-CN',");
            sb.AppendLine("            callbacks: {");
            sb.AppendLine("                onKeyup: function () {");
            sb.AppendLine("                    var content = $(\"#" + this.FieldName + "_editor\").summernote(\"code\");");
            sb.AppendLine("                    $(\"#" + this.FieldName + "\").val(content);");
            sb.AppendLine("                },");
            sb.AppendLine("                onToolbarClick: function (e) {");
            sb.AppendLine("                    var content = $(\"#" + this.FieldName + "_editor\").summernote(\"code\");");
            sb.AppendLine("                    $(\"#" + this.FieldName + "\").val(content);");
            sb.AppendLine("                },");
            sb.AppendLine("                onblur: function (e) {");
            sb.AppendLine("                    var content = $(\"#" + this.FieldName + "_editor\").summernote(\"code\");");
            sb.AppendLine("                    $(\"#" + this.FieldName + "\").val(content);");
            sb.AppendLine("                }");
            sb.AppendLine("            }");
            sb.AppendLine("        });</script>");


            return sb.ToString();
        }
    }
}
